package com.sykj.why.controller.front;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.SolrDocumentT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.SolrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/18
 * @desc Solr 分页查询 模糊查询 全局搜索 控制访问层
 */

@RestController
@RequestMapping("/front/search")
@Slf4j
public class SolrController {

    @Autowired
    SolrService solrService;

    /**
     * 根据检索词 分页查询
     * @param pageDTO
     * @return
     */
    @PostMapping(value = "/findByPage")
    public ResponseEntity findByPage(@RequestBody PageDTO pageDTO) {
        try {
            return Response.success("查询成功", solrService.findByPage(pageDTO));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 添加资源到Solr库
     * @param solrDocumentT
     * @return
     */
    @PostMapping(value = "/add")
    public ResponseEntity add(@RequestBody SolrDocumentT solrDocumentT) {
        try {
            solrService.add(solrDocumentT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
